package com.wynn.dao;

import com.wynn.pojo.EquipError;
import com.wynn.pojo.Equipment;
import com.wynn.pojo.ProductLine;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface EquipDao {

    //根据设备状态计数,用于首页的统计图
    @Select("select count(*) from equipment where status=#{i}")
    int findNumByStatus(int i);

    //通过id查询设备
    @Select("select * from equipment where id=#{id}")
    Equipment selectById(int id);

    //查找所有设备
    @Select("select * from equipment ")
    @Results({
            @Result(column = "productLine",property = "productLine"
                    ,one = @One(select = "com.wynn.dao.ProductLineDao.findProductLineById"))
    })
    List<Equipment> findAll();

    //插入设备
    @Insert({"insert into equipment values(null,#{equipNum},#{equipName},#{standards},#{manufacturer},#{productLine.id},#{status}," +
            "#{operator},#{buyTime},#{assets},#{equipType},#{cycle},#{runTime},#{equipCost}) "})
    void add(Equipment equipment);

    @Delete("delete from equipment where id=#{id}")
    void delete(int id);

    //根据id更新设备
    @Update("update equipment set equipNum=#{equipNum},equipName=#{equipName},standards=#{standards},manufacturer=#{manufacturer},productLine=#{productLine.id},status=#{status},"+
            "operator=#{operator},buyTime=#{buyTime},assets=#{assets},equipType=#{equipType},cycle=#{cycle},runTime=#{runTime},equipCost=#{equipCost} where id=#{id}")
    void edit(Equipment equipment);

    //查询所有设备名称,用于下拉框搜索
    @Select("select distinct equipName from equipment ")
    List<String> getEquipName();


    //按条件搜索设备
    @Select({"<script>" ,
            "select * from equipment",
            "<where>",
            "<if test='equipNum!=null'> equipNum=#{equipNum}</if>",
            "<if test='name!=null'> and equipName=#{name}</if>",
            "<if test='line!=0'>and productLine=#{line}</if>",
            "</where>",
            "</script>"})
    @Results({
            @Result(column = "productLine",property = "productLine"
                    ,one = @One(select = "com.wynn.dao.ProductLineDao.findProductLineById"))
    })
    List<Equipment> search(@Param("name")String name, @Param("line")int line,@Param("equipNum")String equipNum);

    //查询故障记录
    @Select({"select * from equiperrors"})
    @Results({
            @Result(column = "equipId",property = "equipment"
                    ,one = @One(select = "com.wynn.dao.EquipDao.selectById")),
            @Result(column = "productLineId",property = "productLine"
                    ,one = @One(select = "com.wynn.dao.ProductLineDao.findProductLineById"))
    })
    List<EquipError> selectEquipError();

    //查询正常(非故障)的设备
    @Select("select * from equipment where status!=-1")
    List<Equipment> selectEquipAva();

    @Insert({"insert into equiperrors values(null,#{equipment.id},#{productLine.id},#{reportPerson},null" +
            ",#{createDate},#{status},#{content})"})
    void addEquipError(EquipError equipError);

    @Select("select * from equipment where equipNum=#{equipNum}")
    ProductLine findPLidByEquip(String equipNum);

    @Update("update equiperrors set equipId=#{equipment.id},productLineId=#{productLine.id},reportPerson=#{reportPerson},confirmPerson=#{confirmPerson},createDate=#{createDate},content=#{content},status=#{status} where id =#{id}")
    void editEquipError(EquipError equipError);

    @Update("update equipment set status=#{status} where equipNum=#{equipNum}")
    void recoverEquip(Equipment equipment);

    @Delete("delete from equiperrors where id =#{id}")
    void delEquipError(int id);

    //条件搜寻设备故障记录
    @Select({"<script>" ,
            "select * from equiperrors,equipment",
            "<where>",
            "equipment.id=equiperrors.equipId",
            "<if test='equipNum!=null'> and equipNum=#{equipNum}</if>",
            "<if test='status!=null'> and equiperrors.status=#{status}</if>",
            "<if test='lineId!=0'>and productLineId=#{lineId}</if>",
            "</where>",
            "</script>"})
    @Results({
            @Result(column = "equipId",property = "equipment"
                    ,one = @One(select = "com.wynn.dao.EquipDao.selectById")),
            @Result(column = "productLine",property = "productLine"
                    ,one = @One(select = "com.wynn.dao.ProductLineDao.findProductLineById"))
    })
    List<EquipError> equipErrorSearch(@Param("equipNum")String equipNum, @Param("lineId")int lineId, @Param("status")String status);
}
